package beastick.utility;

//Copyright 2008 Tofig Kareemov, estereos@gmail.com
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import beastick.configurator.SConfigurator;

public class CDateFormat {

	public static long TO_MILLISECONDS=1000;
	public static Long HOUR_INTERVAL=new Long(3600);
	public static Long HOURS_IN_DAY=new Long(24);

	
	public static String dateFormat=null;
	public static String dotNetdateFormat=null;
	public static String scheduleDateFormat=null;
	public static String scheduleListDateFormat = null;
	public static String scheduleListShortDateFormat = null;
	public static String cultureInfo = null;
	
	private static CDateFormat instance=null;
	
	private CDateFormat()
	{
	}

	public static CDateFormat getInstance()
	{
		if(instance==null)
		{
			instance=new CDateFormat();
			dateFormat =SConfigurator.GetString("beastick.utility.CDateFormat.dateFormat");
			dotNetdateFormat=SConfigurator.GetString("beastick.utility.CDateFormat.dotNetdateFormat");
			scheduleDateFormat = SConfigurator.GetString("beastick.utility.CDateFormat.scheduleDateFormat");
			cultureInfo = SConfigurator.GetString("beastick.utility.CDateFormat.cultureInfo");
			scheduleListDateFormat = SConfigurator.GetString("beastick.utility.CDateFormat.scheduleListDateFormat");
			scheduleListShortDateFormat = SConfigurator.GetString("beastick.utility.CDateFormat.scheduleListShortDateFormat");
		}
		return instance;
	}
	

	
	private String getDateFormat()
	{
		return dateFormat;
	}

	public DateFormat getDateFormatInstance()
	{
		return new SimpleDateFormat(getDateFormat());
	}

	
	/*
	 * Convert java time to .NET time
	 */
	public static double javaToOADate(Calendar time)
	{
		Calendar SaDec301899 = Calendar.getInstance();
		SaDec301899.set(1900,Calendar.JANUARY,-1,0,0,0); //Set time to: Sat Dec 30 00:00:00 IST 1899
		
		long diff = time.getTimeInMillis()-SaDec301899.getTimeInMillis();
		double days = (((double)diff)/HOURS_IN_DAY.longValue()/HOUR_INTERVAL.longValue()/TO_MILLISECONDS);
		return days;
	}

	public String getDotNetdateFormat() {
		return dotNetdateFormat;
	}
	
	public String getScheduleDateFormat() {
		return scheduleDateFormat;
	}
	
	public String getCultureInfo() {
		return cultureInfo;
	}

}
